package lock;

public class ObjectX {

    public static void main(String[] args) {

        Object lock = new Object();

        new Thread(() -> {
            synchronized (lock) {
                try {
                    System.out.println("Thread-A 获得锁");
                    System.out.println("Thread-A 让出锁");
                    lock.wait();
                    System.out.println("Thread-A 获得锁");
                    System.out.println("Thread-A 释放锁");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();

        new Thread(() -> {
            try {
                synchronized (lock) {
                    System.out.println("Thread-B 获得锁");
                    Thread.sleep(2000);
                    System.out.println("Thread-B 释放锁");
                    System.out.println("Thread-B 通知竞争锁");
                    lock.notifyAll();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();

        new Thread(() -> {
            try {
                synchronized (lock) {
                    System.out.println("Thread-C 获得锁");
                    System.out.println("Thread-C 释放锁");
                    System.out.println("Thread-C 通知竞争锁");
                    lock.notifyAll();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
    }
}
